<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 中奖记录
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="searchData.keywords"
          placeholder="筛选关键词"
          class="handle-input mr10"
          @keyup.native="keyUp($event)"
        ></el-input>
        <el-select v-model="searchData.department_id" placeholder="筛选部门" class="handle-select mr10">
          <!-- <el-option label='筛选部门' value=""></el-option> -->
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="(item,index) in departData"
            :key="index"
          ></el-option>
        </el-select>
        <el-select v-model="searchData.prize_id" placeholder="筛选奖项" class="handle-select mr10">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="(item,index) in prizeData"
            :key="index"
          ></el-option>
        </el-select>
        <el-button type="primary" icon="search" @click="search">搜索</el-button>
        <el-button type="primary" class="fr" @click="delVisible = true">清空中奖记录</el-button>
      </div>
      <el-table
        :data="logData"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
        <el-table-column prop="id" label="ID" sortable width="70" align="center"></el-table-column>
        <el-table-column prop="name" label="用户" width="150" align="center"></el-table-column>
        <el-table-column prop="department_name" label="部门" width="150" align="center"></el-table-column>
        <el-table-column prop="prize_text" label="奖项" width="150" align="center"></el-table-column>
        <el-table-column prop="award_text" label="奖品" width="150" align="center"></el-table-column>
        <el-table-column prop="create_time" label="添加时间" align="center"></el-table-column>
        <el-table-column prop="is_receive" label="是否领取" width="100" align="center"></el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="delVisible"
      width="20%"
      top="30vh"
      center
      style="text-align:center"
    >
      <span>确认删除中奖记录吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="clearLottory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/userApi.js";
export default {
  name: 'basetable',
  data() {
    return {
      url: './static/vuetable.json',
      logData: [],
      awardData: [],
      prizeData: [],
      departData: [],
      cur_page: 1,
      total:0,
      multipleSelection: [],
      searchData: {
        prize_id: '',
        department_id: '',
        keywords: '',
      },
      select_cate: '',
      select_word: '',
      del_list: [],
      is_search: false,
      editVisible: false,
      delVisible: false,
      form: {
        name: '',
        date: '',
        address: ''
      },
      idx: -1
    }
  },
  mounted() {
    // this.getData();
    this.getOther()
  },
  methods: {
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getData(val);
    },
    getData(page) {
      api.winningLog({
        page:page
      })
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.logData = res.list.data
          if (!page) {
            this.total = res.total
          }
          this.logData.forEach(d => {
            this.prizeData.forEach(d1 => {
              if (d.prize_id == d1.id) {
                d.prize_text = d1.name
              }
            })
            this.awardData.forEach(d1 => {
              if (d.award_id == d1.prize_id) {
                d.award_text = d1.name
              }
            })
            this.departData.forEach(d1 => {
              if (d1.id == d.department_id) {
                d.department_name = d1.name
              }
            })
            if (d.is_receive == 0) {
              d.is_receive = '未领取'
            } else {
              d.is_receive = '已领取'
            }
          })
        })


    },
    async getOther() {
      await api.getPrizeList()
        .then((res) => {
          this.prizeData = res.data.list;
          console.log(res, '奖项');
        })

      await api.getAwardList()
        .then((res) => {
          this.awardData = res.data.list;
          console.log(res, '奖品');
        })
      await api.getDepartList()
        .then((res) => {
          this.departData = res.data.list
          console.log(res, '部门');
        })
      await this.getData();
    },
    keyUp(e) {
      if (e.keyCode == 13) {
        this.search()
      }
    },
    search() {
      this.is_search = true;
      console.log(this.searchData);
      api.winningLog(this.searchData)
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.logData = res.list.data
          console.log(res);
        })
    },
    formatter(row, column) {
      return row.address;
    },
    filterTag(value, row) {
      return row.tag === value;
    },
    handleEdit(index, row) {
      this.idx = index;
      const item = this.data[index];
      console.log(item)
      this.form = {
        name: item.name,
        // date: item.date,
        // address: item.address
      }
      this.editVisible = true;
    },
    handleDelete(index, row) {
      this.idx = index;
      this.delVisible = true;
    },
    delAll() {
      const length = this.multipleSelection.length;
      let str = '';
      this.del_list = this.del_list.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
      }
      this.$message.error('删除了' + str);
      this.multipleSelection = [];
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 保存编辑
    saveEdit() {
      this.$set(this.tableData, this.idx, this.form);
      this.editVisible = false;
      this.$message.success(`修改第 ${this.idx + 1} 行成功`);
    },
    // 确定删除
    deleteRow() {
      this.tableData.splice(this.idx, 1);
      this.$message.success('删除成功');
      this.delVisible = false;
    },
    clearLottory() {
      api.winningEmpty()
        .then(res => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.$message.success('清除成功')
          this.delVisible = false
          this.getData()
        })
    }
  }
}

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 200px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
</style>
